package com.googlecode.maratische.google.dao.impl;

import java.io.StringReader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.Types;

import com.googlecode.maratische.google.BaseException;
import com.googlecode.maratische.google.dao.SpamDao;
import com.googlecode.maratische.google.model.Item;
import com.googlecode.maratische.google.model.Spam;

public class SpamDaoImpl extends BaseDaoImpl<Spam> implements SpamDao {

	public SpamDaoImpl(Connection conn) {
		super(conn);
	}

	@Override
	protected PreparedStatement putInsertPrepareStatement(PreparedStatement pStat, Spam status) throws SQLException {
		pStat.setString(1, status.getTitle());
		pStat.setInt(2, status.getTitleType());
		pStat.setString(3, status.getDescription());
		pStat.setInt(4, status.getDescriptionType());
		pStat.setTimestamp(5, new Timestamp(status.getDateStop().getTime()));
		pStat.setInt(6, status.getCount());
		return pStat;
	}

	@Override
	protected PreparedStatement putUpdatePrepareStatement(PreparedStatement pStat, Spam status) throws SQLException {
		pStat = putInsertPrepareStatement(pStat, status);
		pStat.setInt(7, status.getId());
		return pStat;
	}

	@Override
	public Spam createByRow(ResultSet rs) throws SQLException, BaseException {
		Spam item = new Spam();
		item.setId(rs.getInt("ID"));
		item.setTitle(rs.getString("TITLE"));
		item.setTitleType(rs.getInt("TITLE_TYPE"));
		item.setDescription(rs.getString("DESCRIPTION"));
		item.setDescriptionType(rs.getInt("DESCRIPTION_TYPE"));
		item.setDateStop(rs.getDate("DATE_STOP"));
		item.setCount(rs.getInt("COUNT"));
		return item;
	}

	@Override
	protected String getTableName() {
		return "SPAM";
	}

	@Override
	protected String generateUpdateSql() {
		return "update "+getTableName()+" set TITLE = ?,TITLE_TYPE = ?,DESCRIPTION = ?, DESCRIPTION_TYPE = ?, DATE_STOP = ?, COUNT = ? "+
		" where ID = ? ";
	}

	@Override
	protected String generateInsertSql() {
		return "insert into "+getTableName()+" (TITLE, TITLE_TYPE,DESCRIPTION,DESCRIPTION_TYPE, DATE_STOP, COUNT) values (?,?,?,?,?,?)";
	}

}
